SSM框架整合(3)—— SpringMVC框架

具体内容请点击阅读全文…

SpringMVC框架

结构目录

  • java
    • controller
      • AccountController.java(表现层)
  • resources
    • applicationContext.xml(Spring配置文件)
    • springmvc.xml(SpringMVC配置文件)
  • webapp
    • WEB-INF
      • pages
        • list.jsp(jsp页面文件)
      • web.xml(web配置文件)
    • index.jsp(jsp页面文件)

web配置文件

  • 前端控制器
    • 配置 前端控制器(DispatcherServlet)
    • 配置 创建前端控制器时,加载配置文件(contextConfigLocation)
    • 配置 服务器启动时,立即创建前端控制器(1)
    • 配置 映射/作用范围(/)
  • 过滤器
    • 配置 过滤器(CharacterEncodingFilter)
    • 配置 编码(UTF-8)
    • 配置 映射/作用范围(/*)

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

SpringMVC配置文件

  • 开启 注解扫描(context:component-scan)
    • 只扫描表现层注解
  • 配置 视图解析器(InternalResourceViewResolver)
  • 配置 前端控制器(mvc:resource)
  • 开启 SpringMVC注解支持(mvc:annotation-driven)

springmvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 开启 注解扫描 -->
<context:component-scan base-package="cn.water.controller"/>

<!-- 配置 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 配置 前端控制器 -->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>

<!-- 开启 SpringMVC注解支持 -->
<mvc:annotation-driven/>

</beans>

表现层

AccountController.java

  • 返回:list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package cn.water.controller;

import cn.water.domain.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("accountController")
public class AccountController {

@RequestMapping("findAll")
public String findAll(){
System.out.println("表现层:查询所有用户信息");
return "list";
}

}

jsp页面

index.jsp

  • 执行表现层的方法,获得返回值“/WEB-INF/pages/list.jsp”,跳转页面
1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3><a href="accountController/findAll">findAll</a></h3>
</body>
</html>

jsp页面

  • 展示

list.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户信息列表</h1>
</body>
</html>

#

-------------本文结束-------------
Donate comment here